/*
 * @(#)GonuAIInterceptor.java $version 2013. 9. 9.
 *
 */

package com.pace.gonu.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.pace.gonu.ai.GonuAI;
import com.pace.gonu.ai.GonuAIFactory;
import com.pace.gonu.api.UserInputService;
import com.pace.gonu.constants.Order;
import com.pace.gonu.model.chnl.PlayChannel;
import com.pace.gonu.play.PlayService;

/**
 * @author pace
 */
public class GonuAIInterceptor extends HandlerInterceptorAdapter {
	@Autowired
	private PlayService playService;
	@Autowired
	private UserInputService userInputService;

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		HttpSession session = request.getSession();
		Integer chnlNumber = (Integer)session.getAttribute("CHANNEL_NUMBER");

		PlayChannel playChannel = playService.getChannel(chnlNumber);

		if (!playChannel.isPVC() || Order.BLACK.toString().equals(playChannel.getPlayStatusObject().getOrder())) {
			return;
		}

		GonuAI gonuAI = GonuAIFactory.getInstance(playChannel.getGameTypeObject());
		gonuAI.play(playChannel);

		userInputService.changeOrder(Order.WHITE.toString(), playChannel);

	}
}
